package cn.zshprint.serein.service.impl;

import cn.zshprint.serein.exception.ServiceException;
import cn.zshprint.serein.vo.ShopVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zshprint.serein.entity.Shop;
import cn.zshprint.serein.service.ShopService;
import cn.zshprint.serein.mapper.ShopMapper;
import org.apache.lucene.analysis.shingle.ShingleAnalyzerWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop>
        implements ShopService {

    @Override
    public Shop selectOne(Integer id) {
        return getById(id);
    }

    @Override
    public List<Shop> selectAll() {
        return list();
    }

    @Override
    public List<Shop> selectAllAllow() {
        return list(new QueryWrapper<Shop>()
                .ne("status", -1));
    }

    @Override
    public List<Shop> selectAllBan() {
        return list(new QueryWrapper<Shop>()
                .eq("status", -1));
    }

    @Override
    public Integer saveOne(String name, String username, String image, String description) {
        Shop shop = new Shop();
        shop.setName(name);
        shop.setUsername(username);
        shop.setImage(image);
        shop.setDesc(description);
        shop.setCreateTime(LocalDateTime.now());
        save(shop);
        return shop.getId();
    }

    @Override
    public Integer saveOneByJson(ShopVO shopVO) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopVO, shop);
        shop.setCreateTime(LocalDateTime.now());
        save(shop);
        return shop.getId();
    }

    @Override
    public Integer updateOne(Integer id, String username, String name, String desc, String image) {
        if (name == null && desc == null && image == null) {
            throw new ServiceException("参数错误", 505);
        }
        Shop shop = new Shop();
        shop.setId(id);
        shop.setName(name);
        shop.setUsername(username);
        shop.setDesc(desc);
        shop.setImage(image);
        shop.setUpdateTime(LocalDateTime.now());
        updateById(shop);
        return shop.getId();
    }

    @Override
    public Integer updateOneByJson(Shop shop) {
        shop.setUpdateTime(LocalDateTime.now());
        updateById( shop);
        return shop.getId();
    }

    @Override
    public Integer deleteOne(Integer id) {
        update(new UpdateWrapper<Shop>()
                .eq("id", id)
                .set("status", -1)
                .set("update_time", LocalDateTime.now()));
        return id;
    }

    @Override
    public Integer restore(Integer id) {
        update(new UpdateWrapper<Shop>()
                .eq("id", id)
                .set("status", 0)
                .set("update_time", LocalDateTime.now()));
        return id;
    }

    @Override
    public List<Shop> selectNotOperating() {
        return list(new QueryWrapper<Shop>()
                .eq("status", 0));
    }

    @Override
    public List<Shop> selectYetOperating() {
        return list(new QueryWrapper<Shop>()
                .eq("status", 1));
    }

    @Override
    public Integer open(Integer id) {
        update(new UpdateWrapper<Shop>()
                .eq("id", id)
                .set("status", 1));
        return id;
    }

    @Override
    public Integer close(Integer id) {
        update(new UpdateWrapper<Shop>()
                .eq("id", id)
                .set("status", 0));
        return id;
    }
}




